package com.mcflies.commons.util;

import org.apache.log4j.Logger;
import org.hibernate.Transaction;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

/**
 * 
 * TransactionInterceptor.java
 * 
 * @describe：自定义拦截器(事务监听拦截器)
 * @Copyright(c) 2012-2024 Study,mcfly
 * @author mcfly_jun
 */
public class TransactionInterceptor extends MethodFilterInterceptor {
	private static final long serialVersionUID = 5506178339444923969L;
	private Logger logger = Logger.getLogger(TransactionInterceptor.class);

	@Override
	protected String doIntercept(ActionInvocation invocation) throws Exception {
		String resultCode = "";
		try {
			Transaction trans = HibernateSessionFactory.getSession().beginTransaction();
			resultCode = invocation.invoke();
			trans.commit();
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("【" + TransactionInterceptor.class.getName() + "】类中抛出异常信息为:【" + e.getMessage() + "】的异常");
		} finally {
			HibernateSessionFactory.closeSession();
		}
		return resultCode;
	}
}
